package com.quanroon.ysq.queue.delay_queue;

import cn.hutool.core.date.DateUtil;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author quanroong.ysq
 * @version 1.0.0
 * @description 延时任务执行者
 * @createtime 2021/9/8 22:45
 */
public class DelayConsumer implements Runnable {

    private DelayInfo delayInfo;

    public DelayConsumer(DelayInfo delayInfo){
        this.delayInfo = delayInfo;
    }


    @Override
    public void run() {
        try {
            //System.out.println("开始模拟执行任务，假设执行任务消耗了5s，当前时间：" + DateUtil.formatDateTime(new Date()));
            Thread.sleep(5000);//根据执行任务的时间，如果采用少量则会出现任务执行滞后问题。当上一个任务执行越慢，而延时任务相对集中情况下，就会产生严重的滞后
            LocalDateTime expireLocal = LocalDateTime.parse(DateUtil.formatDateTime(delayInfo.getExpireTime()), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Duration between = Duration.between(expireLocal, LocalDateTime.now());
            System.out.println("任务执行完成：" + delayInfo.toString() +",任务执行耽误时间s："
                    + between.getSeconds());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
